//index.js
//获取应用实例
const app = getApp();
const getToken= require('../../utils/get_token.js');
const getMemId= require('../../utils/memberId.js');
let pNo = 1,openId = '', memberId = '';
const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    thou(that);
  } else {
    await getToken.getToken();
    thou(that);
  }
}
const thou = async (that) => {//token没有失效时执行的代码
 await banner(that);
  indexList(that)//初始加载
  if (openId == '') {
    getToken.getOpenId(function (res) {
      wx.setStorageSync('openId', res.data.openId);
      if (memberId == '') {
        getMemId.getMemId(res.data.openId, function (res) {
          if (res.data.flag == 'true') {
            wx.setStorageSync('memberId', res.data.memberId);
          }
        })
      }
    });
  }
}

const banner = function (that) {//轮播图
  let params = [ 'shop/pptIco/lorB', {"A":"SLP_ZDM_TOP_AD"}]
  getToken.getData(function (res) {
  console.log(res.data)
    that.data.imgUrls = res.data.A.SLP_ZDM_TOP_AD;
    that.data.cLength = that.data.imgUrls.length;
    that.setData({
      imgUrls: that.data.imgUrls,
      cLength: that.data.cLength
    })
  }, ...params)
 
}
const indexList = function (that) {//列表
     if (that.data.boolT)
      {
      that.setData({hidden: false})
      that.data.boolT = false;
      let params = ['shop/pingTuan/sheLinPinBusBaseDetail', {"pageNo":pNo,"pageSize":9,"orderby":4,"busbaseId":"CE040EF1303F4758B9956BD69E414393","memberId":memberId}]
      getToken.getData(function (res) {
        console.log(res.data)
        let rows=res.data.busPinTuan.busGoods;
        if (rows.length > 0) {
          for (let i = 0; i <rows.length; i++) {
            rows[i].img = 'https://www.globalhexi.com/' + rows[i].goodsImageStore;
            rows[i].marketPrice = '￥'+rows[i].marketPrice.toFixed(2);
            that.data.hList.push(rows[i]);
          }
          pNo++;
        }
        setTimeout(function () {
          that.data.boolT = true;
          that.setData({
            hList: that.data.hList,
            hidden: true
          })
        }, 500)
      }, ...params)
    }
}
Page({
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    keyWord: '',
    indicatorDots: true,
    indicatorC: "#fff",
    indicatorAC: "red",
    current: 0,
    cIndex: 1,
    cLength: 1,
    autoplay: true,
    interval: 3500,
    duration: 500,
    circular: true,
    hList: [],
    imgUrls: [],
    hidden: false,
    boolT: true,
    list: [],
    glist: []

  },
  onLoad: function () {
    let that = this;
    pNo = 1;
    memberId = wx.getStorageSync('memberId') || '';
    openId = wx.getStorageSync('openId') || '';
    ckToken(that);
  },
 

  onSlideChangeEnd: function (e) {//轮播图切换
    let that = this;
    that.data.cIndex = e.detail.current + 1;
    that.setData({
      cIndex: that.data.cIndex
    })
  },
  showpage:function(e){
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../goon/goon',
        success: function () {
          setTimeout(function () {that.data.boolT = true;}, 500)
        }
      })
    }
  },
  go_detail: function (e) {//直接去详情
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../paired_detail/paired_detail?scene=' + e.currentTarget.dataset.id,
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
  },
  onReachBottom: function () {//上啦加载更多
    let that = this;
    indexList(that);
  },
  onPullDownRefresh: function (e) {//下拉刷新
    // let that = this;
    // wx.showNavigationBarLoading()//加载动画
  
    // setTimeout(function () {
    //   wx.hideNavigationBarLoading()//隐藏动画
    //   wx.stopPullDownRefresh()//停止加载
    // }, 500)

  }
})
